package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"we-laugh/cache"
	"we-laugh/models"
)

func RoleAddNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))

	data := models.RoleNode{
		Nid: nid,
		Rid: rid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func RoleList(c *gin.Context) {

	uid := c.Query("uid")
	var news []models.Role
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.rid = roles.id
		WHERE
		  user_roles.uid = 1
	*/
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid =  ?", uid).
		Debug().
		Find(&news)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": news,
	})
	return
}

func NodeList(c *gin.Context) {
	nid := c.Query("nid")

	var news []models.Node
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.rid = nodes.id
		WHERE
		  role_nodes.nid = 1

	*/
	models.DB.Table("nodes").
		Joins(" LEFT JOIN role_nodes ON role_nodes.rid = nodes.id").
		Where("role_nodes.nid = ?", nid).
		Debug().
		Find(&news)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": news,
	})
	return
}

func CarAdd(c *gin.Context) {
	carName := c.PostForm("carName")
	carType := c.PostForm("carType")
	carPrice := c.PostForm("carPrice")

	if carName == "" || carType == "" || carPrice == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Car{
		CarName:  carName,
		CarType:  carType,
		CarPrice: carPrice,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	orderCode := c.PostForm("orderCode")
	orderFh := c.PostForm("orderFh")
	orderSh := c.PostForm("orderSh")
	Cid, _ := strconv.Atoi(c.PostForm("cid"))

	if orderCode == "" || orderFh == "" || orderSh == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Order{
		OrderCode: orderCode,
		OrderFh:   orderFh,
		OrderSh:   orderSh,
		Cid:       Cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "添加成功",
	})
	return
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	var news []models.CarOrderList

	offset := (page - 1) * size
	redis_key := "list_" + strconv.Itoa(page)
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if errCount == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"cars.car_name as CarName",
				"cars.car_type as CarType",
				"orders.order_code as OrderCode",
				"orders.order_fh as OrderFh",
			).
			Joins("LEFT JOIN cars ON cars.id = orders.id").
			Offset(offset).
			Limit(size).
			Find(&news)
		jsonData, _ := json.Marshal(news)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(errCount), &news)
	}
	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_name as CarName,
		  cars.car_type as CarType,
		  orders.order_code as OrderCode,
		  orders.order_fh as OrderFh
		FROM
		  orders
		  LEFT JOIN cars ON cars.id = orders.id
	*/
	var count int64
	models.DB.Model(&models.Car{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  news,
		"count": count,
	})
	return
}

func CarDetail(c *gin.Context) {
	id := c.Query("id")

	var news models.CarOrderList
	redis_key := "Detail_" + id
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if errCount == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"cars.car_name as CarName",
				"cars.car_type as CarType",
				"orders.order_code as OrderCode",
				"orders.order_fh as OrderFh",
			).
			Joins("LEFT JOIN cars ON cars.id = orders.id").
			Where("orders.id =?", id).
			Find(&news)
		jsonData, _ := json.Marshal(news)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(errCount), &news)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": news,
	})
	return
}
